package com.yami.shop.rights.multishop.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.bean.event.AddRightsExternalEvent;
import com.yami.shop.bean.param.CustomerReqParam;
import com.yami.shop.common.response.ServerResponseEntity;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.rights.common.dto.ProdRightsDto;
import com.yami.shop.rights.common.model.ProdRightsExternalData;
import com.yami.shop.rights.common.model.RightsExternal;
import com.yami.shop.rights.common.service.ProdRightsExternalDataService;
import com.yami.shop.rights.common.service.RightsExternalService;
import com.yami.shop.security.common.util.AuthUserContext;
import com.yami.shop.security.multishop.util.SecurityUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;


@RestController
@RequestMapping("/p/rightsExternal")
@Tag(name = "商品权益绑定信息")
@Slf4j
public class RightsExternalController {

    @Autowired
    private RightsExternalService rightsExternalService;
    @Autowired
    private ProdRightsExternalDataService prodRightsExternalService;
    @Autowired
    private ApplicationContext applicationContext;

    @GetMapping("/list")
    @Operation(summary = "商品权益绑定列表")
    public ServerResponseEntity<IPage<RightsExternal>> pageList(ProdRightsDto vo, PageParam<RightsExternal> page) {
        vo.setShopId(SecurityUtils.getShopUser().getShopId());
        IPage<RightsExternal> pageData = rightsExternalService.pageList(page, vo);
        return ServerResponseEntity.success(pageData);
    }

    @PostMapping("/save")
    @Operation(summary = "新增/修改商品权益关联信息")
    public ServerResponseEntity<Void> saveOrUpdate(@RequestBody RightsExternal dto) {
        dto.setShopId(SecurityUtils.getShopUser().getShopId());
        rightsExternalService.saveOrUpdate(dto);
        applicationContext.publishEvent(new AddRightsExternalEvent(dto.getId(),dto.getExternalCode()));
        return ServerResponseEntity.success();
    }

    @DeleteMapping
    @Operation(summary = "删除商品权益关联信息")
    @Transactional
    public ServerResponseEntity<Void> deleteBrand(@RequestParam Long id) {
        // 删除权益管理表信息
        UpdateWrapper<ProdRightsExternalData> prodRightExternalUpdate = new UpdateWrapper<>();
        prodRightExternalUpdate.eq("external_id", id);
        prodRightExternalUpdate.ne("status", -1);
        prodRightExternalUpdate.set("status", -1);
        prodRightsExternalService.update(prodRightExternalUpdate);

        // 删除权益关联主表
        UpdateWrapper<RightsExternal> prodRightUpdate = new UpdateWrapper<>();
        prodRightUpdate.eq("id", id);
        prodRightUpdate.ne("status", -1);
        prodRightUpdate.set("status", -1);
        rightsExternalService.update(prodRightUpdate);
        return ServerResponseEntity.success();
    }

    @GetMapping("/info/{id}")
    @Operation(summary = "获取商品权益关联信息")
    @Parameter(name = "id", description = "权益主键", required = true)
    public ServerResponseEntity<RightsExternal> info(@PathVariable("id") Long id) {

        return ServerResponseEntity.success(rightsExternalService.getById(id));
    }

    @GetMapping("/getList")
    @Operation(summary = "商品权益绑定列表")
    public ServerResponseEntity<List<RightsExternal>> getList() {
        QueryWrapper<RightsExternal> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("shop_id", AuthUserContext.get().getShopId());
        queryWrapper.eq("status", 0);
        queryWrapper.orderByDesc("create_time");
        return ServerResponseEntity.success(rightsExternalService.list(queryWrapper));
    }


    @GetMapping("/excelExternal")
    @Operation(summary =  "导出券信息", description = "导出券信息")
    public void excelExternal(@RequestParam Long id, HttpServletResponse response) {
        rightsExternalService.excelExternal(id, response);
    }


}
